#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import jsonify
from flask_restful import Resource, reqparse

from service.apiKeyService import ApiKeyService
from utils import commons
from utils.response_code import RET, error_map_EN


class ApiKeyOtherResource(Resource):
    @classmethod
    def add_api_key(cls):
        """
            添加api key
        """
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', location='form', required=True, help='user_id参数类型不正确或缺失')
        parser.add_argument('content', location='form', required=True, help='content参数类型不正确或缺失')
        parser.add_argument('chat_model', location='form', required=True, help='chat_model参数类型不正确或缺失')
        parser.add_argument('settings', location='form', required=False, help='settings参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = ApiKeyService.add_api_key(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def change_api_key(cls):
        """
            修改用户当前api key 及其模型
        """

        parser = reqparse.RequestParser()
        parser.add_argument('key_id', location='form', required=True, help='key_id参数类型不正确或缺失')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = ApiKeyService.change_current_api_key(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def delete_api_key(cls, key_id=None):
        """
            删除api key
        """
        if key_id:
            kwargs = {
                'key_id': key_id
            }

        else:
            return jsonify(code=RET.PARAMERR, message=error_map_EN[RET.PARAMERR], data='id不能为空')

        res = ApiKeyService.delete_api_key(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def get_current_model(cls):
        """
            获取当前模型
        """
        res = ApiKeyService.get_current_model()

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def get_model_list(cls):
        """
            获取api key列表
        """
        res = ApiKeyService.get_api_key_list()

        return jsonify(code=res['code'], message=res['message'], data=res['data'])